package com.shuhua.taskServer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.shuhua.pojo.entity.DirOrder;
import com.shuhua.taskServer.service.DirOrderService;
import com.shuhua.common.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/dirorder")
public class DirOrderController {

    @Autowired
    private DirOrderService dirOrderService;

    private static final String[] DIRORDER_FIELDS = {"admin_id", "task_id", "craftsman_id", "state"};
    private static final String[] TASK_FIELDS = {"task_id", "title", "task_type", "description", "address", "start_time", "end_time", "budget", "dispatch_mode", "priority_level", "status", "created_at", "updated_at", "remark"};
    private static final String[] CRAFTSMAN_FIELDS = {"craftsman_username", "craftsman_phone", "craftsman_real_name", "craftsman_department", "craftsman_station", "craftsman_avatar"};

    @PostMapping
    public Result<?> add(@RequestBody DirOrder dirOrder) {
        dirOrderService.save(dirOrder);
        return Result.success();
    }

    @DeleteMapping("/{adminId}/{taskId}/{craftsmanId}")
    public Result<?> delete(@PathVariable Long adminId, @PathVariable Long taskId, @PathVariable Long craftsmanId) {
        QueryWrapper<DirOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_id", adminId).eq("task_id", taskId).eq("craftsman_id", craftsmanId);
        dirOrderService.remove(wrapper);
        return Result.success();
    }

    @PutMapping
    public Result<?> update(@RequestBody DirOrder dirOrder) {
        QueryWrapper<DirOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_id", dirOrder.getAdminId()).eq("task_id", dirOrder.getTaskId()).eq("craftsman_id", dirOrder.getCraftsmanId());
        dirOrderService.update(dirOrder, wrapper);
        return Result.success();
    }

    @GetMapping("/{adminId}/{taskId}/{craftsmanId}")
    public Result<Map<String, Object>> get(@PathVariable Long adminId, @PathVariable Long taskId, @PathVariable Long craftsmanId) {
        Map<String, Object> result = dirOrderService.getDirOrderWithTask(adminId, taskId, craftsmanId);
        return Result.success(wrapDirOrder(result));
    }

    @GetMapping("/list")
    public Result<List<Map<String, Object>>> list() {
        List<Map<String, Object>> list = dirOrderService.getAllDirOrderWithTask();
        list.replaceAll(this::wrapDirOrder);
        return Result.success(list);
    }

    @GetMapping("/craftsman/{craftsmanId}")
    public Result<List<Map<String, Object>>> getByCraftsmanId(@PathVariable Long craftsmanId) {
        List<Map<String, Object>> list = dirOrderService.getDirOrderWithTaskByCraftsmanId(craftsmanId);
        list.replaceAll(this::wrapDirOrder);
        return Result.success(list);
    }

    private Map<String, Object> wrapDirOrder(Map<String, Object> map) {
        if (map == null) return null;
        java.util.LinkedHashMap<String, Object> result = new java.util.LinkedHashMap<>();
        java.util.LinkedHashMap<String, Object> task = new java.util.LinkedHashMap<>();
        java.util.LinkedHashMap<String, Object> craftsman = new java.util.LinkedHashMap<>();
        // 主表字段
        for (String field : DIRORDER_FIELDS) {
            if (map.containsKey(field)) result.put(field, map.get(field));
        }
        // 任务字段
        for (String field : TASK_FIELDS) {
            if (map.containsKey(field)) task.put(field, map.get(field));
        }
        // 工匠字段
        for (String field : CRAFTSMAN_FIELDS) {
            if (map.containsKey(field)) craftsman.put(field.substring(10), map.get(field));
        }
        // 其他字段补充
        for (String key : map.keySet()) {
            if (!result.containsKey(key) && !task.containsKey(key) && !key.startsWith("craftsman_")) {
                result.put(key, map.get(key));
            }
        }
        result.put("task", task);
        result.put("craftsman", craftsman);
        return result;
    }
} 